import com.ithema.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Mybatistest {

    @Test
    public void testone() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //构建SqlSession工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂打开SqlSession 拿到session
        SqlSession sqlSession = build.openSession();
        //通过命名空间和方法id 指定的查询语句
        List<User> users = sqlSession.selectList("userMapper.findall");
        System.out.println(users);
        sqlSession.close();

    }
    @Test
    public void testtwo() throws IOException {

        User user = new User();
        user.setUsername("songweixia");
        user.setPassword("521");

        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //构建SqlSession工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂打开SqlSession 拿到session
        SqlSession sqlSession = build.openSession(true);
        //通过命名空间和方法id 指定的查询语句
        sqlSession.insert("userMapper.insert",user);
        //"userMapper.insert"
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testthree() throws IOException {

        User user = new User();
        user.setId(5L);
        user.setUsername("songweixia");
        user.setPassword("521");

        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //构建SqlSession工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂打开SqlSession 拿到session
        SqlSession sqlSession = build.openSession(true);
        //通过命名空间和方法id 指定的查询语句
        sqlSession.update("userMapper.update",user);
        //"userMapper.insert"
        sqlSession.commit();
        sqlSession.close();
    }
   @Test
    public void test4() throws IOException {

        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //构建SqlSession工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂打开SqlSession 拿到session
        SqlSession sqlSession = build.openSession();
        //通过命名空间和方法id 指定的查询语句
        sqlSession.update("userMapper.delete",1);
        //"userMapper.insert"
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test5() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        sqlSession.update("userMapper.delete", 2);
        sqlSession.close();
    }


}
